import fitz

def pdf_to_images(pdf_path, output_path):
    pdf_document = fitz.open(pdf_path)
    dpi = 300
    for page_number in range(pdf_document.page_count):
        page = pdf_document[page_number]
        # image = page.get_pixmap()
        image = page.get_pixmap(matrix=fitz.Matrix(dpi/72, dpi/72))
        image.save(f"{output_path}/page{page_number + 1}.png")
     

        

# 使用示例
pdf_path = f"D:\\Users\\65720\\Desktop\\电信项目-方案\\文艺\\factory-3\\"
output_path = f"D:\\Users\\65720\\Desktop\\电信项目-方案\\文艺\\factory-3"
file_name = f"202204-v1\\page1.png"
# pdf_to_images(f"{pdf_path}\\{file_name}", output_path)


from PIL import Image
from webptools import cwebp


def convert_png_to_webp(input_path, output_path, quality=75):
    cwebp(input_image=input_path, output_image=output_path, option=f"-q {quality}")

# 使用示例
# convert_png_to_webp("input.png", "output.webp", quality=75)


# 使用示例
# convert_png_to_webp("page1.png", "output.webp", quality=75)



from PIL import Image
import os
 # 把图片转为 webp格式
def convert_to_webp(image_path):
    # 打开原始图像文件
    image = Image.open(image_path)
    
    # 获取图像文件名及其路径
    file_name, ext = os.path.splitext(os.path.basename(image_path))
    
    # 构建新的WebP图像文件名
    webp_file_name = f"{file_name}.webp"
    
    try:
        # 将图像保存为WebP格式
        image.save("F:\VscodeProject\pythoneDemo\pdf2png-py\\" + webp_file_name, "WEBP")
        
        print(f"成功将 {image_path} 转换为 WebP 格式！输出文件位于 output/{webp_file_name}")
    except Exception as e:
        print(f"无法将 {image_path} 转换为 WebP 格式：{e}")
 
# 调用函数进行转换
convert_to_webp("F:\VscodeProject\pythoneDemo\pdf2png-py\page1.png")
